﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Odds.Entity;
using Odds.DBUtility;
using System.Data;

namespace Odds.DataAccess
{
    public class OddsIssueItemController
    {
        private string SQL_INSERT = string.Format("INSERT INTO OddsIssueItem({0},{1},{2},{3},{4},{5}) " + 
                                                "VALUES(@{0},@{1},@{2},@{3},@{4},@{5});",
                                                PARM_ISSUEID, PARM_INDEXINISSUE, PARM_HOMETEAMID, 
                                                PARM_VISITINGTEAMID, PARM_MATCHTYPEID, PARM_MATCHRESULT);
        
        private const string SQL_CONDITION_UNDELETED = " IsDeleted = 0 ";

        private const string PARM_ISSUEID = "IssueId";
        private const string PARM_INDEXINISSUE = "IndexInIssue";
        private const string PARM_HOMETEAMID = "HomeTeamID";
        private const string PARM_VISITINGTEAMID = "VisitingTeamID";
        private const string PARM_MATCHTYPEID = "MatchTypeID";
        private const string PARM_MATCHRESULT = "MatchResult";


        public Dictionary<int, Guid> Insert(List<OddsIssueItem> oddsIssueItemSet)
        {
            Dictionary<int, Guid> result = new Dictionary<int, Guid>();
            try
            {
                StringBuilder sql = new StringBuilder();
                foreach (OddsIssueItem oddsIssueItem in oddsIssueItemSet)
                {
                    sql.AppendFormat("EXEC Pro_OddsIssueItemInsert '{0}','{1}',{2},'{3}','{4}','{5}',{6};",
                                        oddsIssueItem.IssueItemID, oddsIssueItem.IssueID, oddsIssueItem.IndexInIssue, 
                                        oddsIssueItem.HomeTeamID, oddsIssueItem.VisitingTeamID, oddsIssueItem.MatchTypeID, 
                                        oddsIssueItem.MatchResult);
                }
                sql.Append(@"SELECT IndexInIssue,IssueItemID FROM OddsIssueItem WHERE IssueID = '" + 
                    oddsIssueItemSet[0].IssueID + "' AND " + SQL_CONDITION_UNDELETED);
                result = OddsSQLHelper.GetIntKeyDictionary(sql.ToString());
            }
            catch(Exception e)
            {
                throw e;
            }
            return result;
        }

        public bool IssueItemUpdate(List<OddsResult> oddsResultSet)
        {
            bool result = false;
            StringBuilder sql = new StringBuilder();
            foreach (OddsResult o in oddsResultSet)
            {
                StringBuilder matchResult = new StringBuilder();
                foreach (short s in o.OddsIssueItemMatchResult)
                {
                    matchResult.AppendFormat("{0},", s.ToString());
                }
                sql.AppendFormat("EXEC Pro_OddsIssueItemUpdate {0},{1}{2},{3},{4};",
                    o.OddsIssueRealID, matchResult, o.Win14, o.Win13, o.Win9);
            }
            OddsSQLHelper.ExecuteNonQuery(CommandType.Text, sql.ToString());
            result = true;
            return result;
        }

        public bool IssueItemResultUpdate(OddsResult oddsResult)
        {
            bool result = false;
            try
            {
                StringBuilder matchResult = new StringBuilder();
                foreach (short s in oddsResult.OddsIssueItemMatchResult)
                {
                    matchResult.AppendFormat("{0},", s.ToString());
                }
                matchResult.Remove(matchResult.ToString().LastIndexOf(','), 1);
                OddsSQLHelper.ExecuteNonQuery(CommandType.Text,
                    string.Format("EXEC Pro_OddsIssueItemResultUpdate {0},{1};", 
                        oddsResult.OddsIssueRealID, matchResult));

                result = true;
            }
            catch (Exception e)
            {
                throw e;
            }
            return result;
        }

        public List<OddsIssueItem> GetByIssueRealId(int oddsRealId)
        {            
            string sql = "SELECT ii.* FROM OddsIssueItem ii" + 
                        " LEFT JOIN OddsIssue i ON ii.IssueId = i.IssueId " +
                        " WHERE i.IssueRealId = '" + oddsRealId + "'";
            return OddsSQLEntityHelper<OddsIssueItem>.GetEntitySet(sql);
        }
    }
}
